package com.giaothong.ui;

import java.util.ArrayList;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.giaothong.R;
import com.giaothong.customview.adapter.ListLocationAdapter;
import com.giaothong.db.table.DtbLikeLocation;
import com.giaothong.model.entity.LocationSearchEntity;
import com.giaothong.ui.base.BaseActivity;
import com.giaothong.utils.DialogUtil;
import com.giaothong.utils.SessionData;

public class LikedLocationActivity extends BaseActivity {

	public static final String EXTRA_LOCATION_LAT = "location_extra_lat";
	public static final String EXTRA_LOCATION_LON = "location_extra_lon";
	public static final String EXTRA_ACTION = "location_extra_action";

	private ListView lvLikedLocation;
	private ListLocationAdapter adapter;
	private ArrayList<LocationSearchEntity> arrayList = new ArrayList<LocationSearchEntity>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_liked_location);
		init();
		setData();
		setAction();
	}

	/**
	 * init View Id
	 */
	private void init() {
		((Button) findViewById(R.id.btnNext)).setVisibility(View.INVISIBLE);
		((TextView) findViewById(R.id.txtTitleBar)).setText(R.string.like_location);
		lvLikedLocation = (ListView) findViewById(R.id.lvLikedLocation);
		adapter = new ListLocationAdapter(this, android.R.layout.simple_list_item_1, arrayList);
		lvLikedLocation.setAdapter(adapter);
	}

	/**
	 * set Data
	 */
	private void setData() {
		displayLocationFromDatabase();
	}

	/**
	 * set Action view
	 */
	private void setAction() {
		// item onclick
		lvLikedLocation.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// show dialog action
				showDialogChooseLocationFromNavigation(arg2);
			}
		});
	}

	/**
	 * display from database
	 */
	private void displayLocationFromDatabase() {
		String selectQuery = "SELECT * FROM " + DtbLikeLocation.TABLE_LIKE_LOCATION;
		arrayList.addAll(DtbLikeLocation.querySelect(SessionData.getDb(), selectQuery));
		adapter.notifyDataSetChanged();
	}

	/**
	 * show dialog when click item
	 */
	public void showDialogChooseLocationFromNavigation(final int index) {
		int select[] = { R.string.delete, R.string.delete_all, R.string.dan_duong_den_day, R.string.show_in_map };

		DialogUtil.showListDialog(this, R.string.select, select, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// neu chon thong bao
				switch (which) {
				// delete select
				case 0: {
					DialogUtil.showConfirmDialog(LikedLocationActivity.this, R.string.wan_to_delete, R.string.wan_to_delete, new DialogUtil.ConfirmDialogOnClickListener() {

						public void onOKButtonOnClick() {
							DtbLikeLocation.deleteRowOfDatabase(SessionData.getDb(), arrayList.get(index));
							arrayList.remove(index);
							adapter.notifyDataSetChanged();
						}

						public void onCancelButtonOnClick() {
							// nothing...
						}
					});
					break;
				}
				// delete all select
				case 1: {
					DialogUtil.showConfirmDialog(LikedLocationActivity.this, R.string.wan_to_delete_all, R.string.wan_to_delete_all, new DialogUtil.ConfirmDialogOnClickListener() {

						public void onOKButtonOnClick() {
							DtbLikeLocation.deleteAllRowFromDatabase(SessionData.getDb());
							arrayList.clear();
							adapter.notifyDataSetChanged();
						}

						public void onCancelButtonOnClick() {
							// nothing...
						}
					});
					break;
				}
				// navigation select
				case 2: {
                     Intent intent = new Intent();
					intent.putExtra(EXTRA_LOCATION_LAT, arrayList.get(index).getGeoPoint().getLatitudeE6());
					intent.putExtra(EXTRA_LOCATION_LON, arrayList.get(index).getGeoPoint().getLongitudeE6());
					intent.putExtra(EXTRA_ACTION, R.string.dan_duong_den_day);
					setResult(RESULT_OK, intent);
					finish();
					break;
				}
				// show map select
				case 3: {
					Intent intent = new Intent();
					intent.putExtra(EXTRA_LOCATION_LAT, arrayList.get(index).getGeoPoint().getLatitudeE6());
					intent.putExtra(EXTRA_LOCATION_LON, arrayList.get(index).getGeoPoint().getLongitudeE6());
					intent.putExtra(EXTRA_ACTION, R.string.show_in_map);
					setResult(RESULT_OK, intent);
					finish();
					break;
				}
				default:
					break;
				}

			}
		});
	}

	/**
	 * call this activity
	 * 
	 * @param context
	 */
	public static void show(Context context) {
		Intent intent = new Intent(context, LikedLocationActivity.class);
		context.startActivity(intent);
	}
}
